//
//  GBTagListView.m
//  升级版流式标签支持点击
//
//  Created by 张国兵 on 15/8/16.
//  Copyright (c) 2015年 zhangguobing. All rights reserved.
//

#import "GBTagListView.h"
#define HORIZONTAL_PADDING 7.0f
#define VERTICAL_PADDING   3.0f
#define LABEL_MARGIN       20.0f
#define BOTTOM_MARGIN      20.0f
#define KBtnTag            1000
#define R_G_B_16(rgbValue)\
\
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 \
alpha:1.0]
@interface GBTagListView(){
    
    CGFloat _KTagMargin;//左右tag之间的间距
    CGFloat _KBottomMargin;//上下tag之间的间距
    NSInteger _kSelectNum;//实际选中的标签数
    UIButton*_tempBtn;//临时保存对象
    
}
@end
@implementation GBTagListView
-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        _kSelectNum=0;
        totalHeight=0;
        self.frame=frame;
        _tagArr=[[NSMutableArray alloc]init];
        /**默认是多选模式 */
        self.isSingleSelect=NO;
        
    }
    return self;
    
}
-(void)setTagWithTagArray:(NSArray*)arr{
    
    previousFrame = CGRectZero;
    [_tagArr addObjectsFromArray:arr];
    [arr enumerateObjectsUsingBlock:^(NSString*str, NSUInteger idx, BOOL *stop) {
        
        UIButton*tagBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        tagBtn.frame=CGRectZero;
        // 背景:未选中颜色
        _signalTagColor = _signalTagColor?_signalTagColor:[UIColor whiteColor];
        // 背景:选中颜色
        _selectTagColor = _selectTagColor?_selectTagColor:[UIColor greenColor];
        // 字体：未选中颜色
        _textColor = _textColor?_textColor:[UIColor blackColor];
        // 字体：选中颜色
        _selectedTextColor = _selectedTextColor?_selectedTextColor:[UIColor whiteColor];
        // 按钮圆角
        _buttonCornerRadius = _buttonCornerRadius?_buttonCornerRadius:13;
        // 描边：未选中颜色
        _buttonBorderColor = _buttonBorderColor?_buttonBorderColor:[UIColor clearColor];
        // 描边：选中颜色
        _selectedButtonBorderColor = _selectedButtonBorderColor?_selectedButtonBorderColor:[UIColor clearColor];
        _font = _font?_font:[UIFont systemFontOfSize:14];
        tagBtn.backgroundColor=_signalTagColor;
        if(_canTouch){
            tagBtn.userInteractionEnabled=YES;
        }else{
            tagBtn.userInteractionEnabled=NO;
        }
        [tagBtn setTitleColor:_textColor forState:UIControlStateNormal];
        [tagBtn setTitleColor:_selectedTextColor forState:UIControlStateSelected];
        tagBtn.titleLabel.font=_font;
        [tagBtn addTarget:self action:@selector(tagBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [tagBtn setTitle:str forState:UIControlStateNormal];
        tagBtn.tag=KBtnTag+idx;
        tagBtn.layer.cornerRadius=_buttonCornerRadius;
        tagBtn.layer.borderColor=self.buttonBorderColor.CGColor;
        tagBtn.layer.borderWidth=0.5;
        tagBtn.clipsToBounds=YES;
        NSDictionary *attrs = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:15]};
        CGSize Size_str=[str sizeWithAttributes:attrs];
        
        Size_str.width += HORIZONTAL_PADDING*3;
        Size_str.height += VERTICAL_PADDING*3;
        if (self.isNewVERTICAL_PADDING) {
            Size_str.height += 4*3;
        }
        
        // 按钮固定宽度
        if (self.tagButtonWidth) {
            Size_str.width = self.tagButtonWidth;
        }
        // 按钮固定高度
        if (self.tagButtonHight) {
            Size_str.height = self.tagButtonHight;
            
        }
        
        CGRect newRect = CGRectZero;
        
        if(_KTagMargin&&_KBottomMargin){
            //之后的几排
            if (previousFrame.origin.x + previousFrame.size.width + Size_str.width + _KTagMargin > self.bounds.size.width) {
                
                newRect.origin = CGPointMake(0, previousFrame.origin.y + Size_str.height + _KBottomMargin);
                totalHeight +=Size_str.height + _KBottomMargin;
            }
            //第一排
            else {
                if (idx==0) {
                    newRect.origin = CGPointMake(previousFrame.origin.x + previousFrame.size.width , previousFrame.origin.y);
                }else{
                    newRect.origin = CGPointMake(previousFrame.origin.x + previousFrame.size.width + _KTagMargin, previousFrame.origin.y);
                }
                
                
            }
            [self setHight:self andHight:totalHeight+Size_str.height + _KBottomMargin];
            
            
        }else{
            if (previousFrame.origin.x + previousFrame.size.width + Size_str.width + LABEL_MARGIN > self.bounds.size.width) {
                
                newRect.origin = CGPointMake(6, previousFrame.origin.y + Size_str.height + BOTTOM_MARGIN);
                totalHeight +=Size_str.height + BOTTOM_MARGIN;
            }
            else {
                newRect.origin = CGPointMake(previousFrame.origin.x + previousFrame.size.width + LABEL_MARGIN, previousFrame.origin.y);
                
            }
            [self setHight:self andHight:totalHeight+Size_str.height + BOTTOM_MARGIN];
        }
        newRect.size = Size_str;
//        // 按钮固定宽度
//        if (self.tagButtonWidth) {
//            newRect.size.width = self.tagButtonWidth;
//            
//        }
        [tagBtn setFrame:newRect];
        previousFrame=tagBtn.frame;
        [self setHight:self andHight:totalHeight+Size_str.height + BOTTOM_MARGIN];
        [self addSubview:tagBtn];
    }];
    if(_GBbackgroundColor){
        
        self.backgroundColor=_GBbackgroundColor;
        
    }else{
        
        self.backgroundColor=[UIColor whiteColor];
        
    }
}
#pragma mark-改变控件高度
- (void)setHight:(UIView *)view andHight:(CGFloat)hight
{
    CGRect tempFrame = view.frame;
    tempFrame.size.height = hight;
    view.frame = tempFrame;
    self.tagViewHeight = hight;
}
-(void)tagBtnClick:(UIButton*)button{
    NSLog(@"-----%ld----", button.tag);
    if(_isSingleSelect){
        if(button.selected){
            
            button.selected=!button.selected;
            
        }else{
            
            _tempBtn.selected=NO;
            _tempBtn.backgroundColor=_signalTagColor;
            button.selected=YES;
            _tempBtn=button;
            
        }
        
    }else{
        
        button.selected=!button.selected;
    }
    
    if(button.selected==YES){
        button.backgroundColor = _selectTagColor;
        button.layer.borderColor = _selectedButtonBorderColor.CGColor;
    }else if (button.selected==NO){
        button.backgroundColor = _signalTagColor;
        button.layer.borderColor = _buttonBorderColor.CGColor;
    }
    
    [self didSelectItems];
    
    // 回调点击了第几个
    if (self.didSelectIndex) {
        self.didSelectIndex(button.tag - KBtnTag, button.selected);
    }
}
-(void)didSelectItems{
    
    NSMutableArray*arr=[[NSMutableArray alloc]init];
    
    for(UIView*view in self.subviews){
        
        if([view isKindOfClass:[UIButton class]]){
            
            UIButton*tempBtn=(UIButton*)view;
            tempBtn.enabled=YES;
            if (tempBtn.selected==YES) {
                [arr addObject:_tagArr[tempBtn.tag-KBtnTag]];
                _kSelectNum=arr.count;
            }
        }
    }
    if(_kSelectNum==self.canTouchNum){
        
        for(UIView*view in self.subviews){
            
            UIButton*tempBtn=(UIButton*)view;
            
            if (tempBtn.selected==YES) {
                tempBtn.enabled=YES;
                
            }else{
                tempBtn.enabled=NO;
                
            }
        }
    }
    
    // 回调数组
    if (_didselectItemBlock) {
       self.didselectItemBlock(arr);
    }
    
}
-(void)setMarginBetweenTagLabel:(CGFloat)Margin AndBottomMargin:(CGFloat)BottomMargin{
    
    _KTagMargin=Margin;
    _KBottomMargin=BottomMargin;
    
}

/** 选中的数组 */
- (void)putSelectedArray:(NSArray *)selectedArray {
    for (NSString *titleString in selectedArray) {
        for(UIView*view in self.subviews){
            if([view isKindOfClass:[UIButton class]]){
                UIButton *tempBtn=(UIButton*)view;
                if ([tempBtn.titleLabel.text isEqualToString:titleString]) {
                    [self tagBtnClick:tempBtn];
                }
            }
        }
    }
}

@end
